package com.work.music.dao;

import com.work.music.entity.AdminLog;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface AdminLogMapper {
    @Delete({
        "delete from admin_log",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into admin_log (admin_id, addtime, ",
        "addip, type)",
        "values (#{adminId,jdbcType=BIGINT}, #{addtime,jdbcType=TIMESTAMP}, ",
        "#{addip,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(AdminLog record);

    @Select({
        "select",
        "id, admin_id, addtime, addip, type",
        "from admin_log",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="admin_id", property="adminId", jdbcType=JdbcType.BIGINT),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER)
    })
    AdminLog selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, admin_id, addtime, addip, type",
        "from admin_log"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="admin_id", property="adminId", jdbcType=JdbcType.BIGINT),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER)
    })
    List<AdminLog> selectAll();

    @Update({
        "update admin_log",
        "set admin_id = #{adminId,jdbcType=BIGINT},",
          "addtime = #{addtime,jdbcType=TIMESTAMP},",
          "addip = #{addip,jdbcType=VARCHAR},",
          "type = #{type,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(AdminLog record);

    @Select("select ifnull(count(*), 0) from admin a,admin_log l ${msg} and a.id=l.admin_id")
    int selectLogCount(@Param("msg") String msg);

    @Select("select a.admin_name adminName,l.id,l.addtime,l.addip,l.type from admin a,admin_log l ${msg} and a.id=l.admin_id " +
            "order by l.addtime desc limit #{limit},#{offset}")
    List<Map<String,Object>> selectLogList(@Param("msg") String msg, @Param("limit") int limit, @Param("offset") int offset);
}